package com.lsfb.signln.Statistics;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lsfb.signln.Leave.PersonnelSelect.PersonnelSelectActivity;
import com.lsfb.signln.Login.LoginActivity;
import com.lsfb.signln.My.MyDetailsBean;
import com.lsfb.signln.R;
import com.lsfb.signln.Statistics.DateStatistics.DateStatisticsActivity;
import com.lsfb.signln.Statistics.EvectionSearch.EvectionSearchActivity;
import com.lsfb.signln.Statistics.HistoryRecord.HistoryRecordActivity;
import com.lsfb.signln.Statistics.LeaveNum.LeaveNumActivity;
import com.lsfb.signln.Statistics.LeaveType.LeaveTypeStatisticsActivity;
import com.lsfb.signln.Statistics.WithoutAddress.WithoutAddressActivity;
import com.lsfb.signln.Statistics.WithoutDepNum.WithoutDepNumActivity;
import com.lsfb.signln.Utils.LittleUtils;
import com.lsfb.signln.Utils.LsfbTimePicker;
import com.lsfb.signln.Utils.PopWindowManager;
import com.lsfb.signln.Utils.URLString;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbFragment;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInjectUtils;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.T;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * Created by Administrator on 2018/1/23 0023.
 */

public class StatisticsFragment extends LsfbFragment {

    private View view;

    /**
     * activity对象
     */
    private Activity mactivity;

    @ViewInject(R.id.fg_leave_top)
    private LinearLayout fg_leave_top;

    @ViewInject(R.id.fg_text_statistics_date)
    private TextView text_statistics_time;

    /**
     * 统计对象
     */
    @ViewInject(R.id.fg_text_statistics_person)
    private TextView text_statistics_person;

    /**
     * 统计时间
     */
    @ViewInject(R.id.fg_text_statistics_date)
    private TextView text_statistics_date;

    /**
     * 内勤定位数
     */
    @ViewInject(R.id.fg_text_inside_location_num)
    private TextView text_inside_location_num;

    /**
     * 外勤定位数
     */
    @ViewInject(R.id.fg_text_out_location_num)
    private TextView text_out_location_num;

    /**
     * 外勤补录数
     */
    @ViewInject(R.id.fg_text_out_repair_num)
    private TextView text_out_repair_num;

    /**
     * 外勤定位率
     */
    @ViewInject(R.id.fg_text_out_location_odds)
    private TextView text_out_location_odds;

    /**
     * 请假次数
     */
    @ViewInject(R.id.fg_text_leave_num)
    private TextView text_leave_num;

    /**
     * 外勤补录率
     */
    @ViewInject(R.id.fg_text_out_repair_odds)
    private TextView text_out_repair_odds;

    /**
     * PopWindow管理类
     */
    private PopWindowManager replay;

    //timePicker
    private LsfbTimePicker starttime, endtime;

    //人员类型
    private String usertype = "0";
    //下属人员
    private String users = null;
    //部门
    private String depars = null;
    //时间类型
    private int timetype = 0;
    //开始时间
    private String start_time = "";
    //结束时间
    private String end_time = "";
    private int jobs;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_statistics, null);
            ViewInjectUtils.inject(this, view);

            initPop();

            starttime = new LsfbTimePicker(mactivity);
            starttime.SelectSureListenner(new LsfbTimePicker.ClickSureListener() {
                @Override
                public void ClickSure(String time) {
                    if (!end_time.isEmpty()) {
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                        try {
                            Date start_date = df.parse(time);
                            Date end_date = df.parse(end_time);
                            if (end_date.getTime() < start_date.getTime()) {
                                //结束时间小于开始时间
                                T.showShort(mactivity, "开始时间不能大于结束时间");
                                return;
                            }
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                    start_time = time;
                    showPop(start_time, end_time, timetype);
                }
            });

            endtime = new LsfbTimePicker(mactivity);
            endtime.SelectSureListenner(new LsfbTimePicker.ClickSureListener() {
                @Override
                public void ClickSure(String time) {
                    if (!start_time.isEmpty()) {
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                        try {
                            Date start_date = df.parse(start_time);
                            Date end_date = df.parse(time);
                            if (end_date.getTime() < start_date.getTime()) {
                                //结束时间小于开始时间
                                T.showShort(mactivity, "结束时间不能小于开始时间");
                                return;
                            }
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                    end_time = time;
                    showPop(start_time, end_time, timetype);
                }
            });
        }
        return view;
    }

    /**
     * 获取我的身份
     */
    private void getMyInfo() {
        HashMap hashMap = new HashMap<>();
        new BaseInternet().getData_Get(URLString.INFO, hashMap, MyDetailsBean.class, new MyInformationEvent<MyDetailsBean>(), true, false);
    }

    @EventAnnotation
    public void eventResult(MyInformationEvent<MyDetailsBean> event) {
        switch (event.getCode()) {
            case 200:
                jobs = event.getData().getJobs();
                break;
            case 1006:
                T.showShort(mactivity, "您的账号已在其它设备登录，请重登录");
                LittleUtils.exit(mactivity);
                startActivity(new Intent(mactivity, LoginActivity.class));
                break;
            default:
                T.showShort(getActivity(), event.getMsg());
                break;
        }
    }

    /**
     * 获取统计基本信息
     */
    private void getInfo() {
        HashMap hashMap = new HashMap();
        hashMap.put("type", usertype);
        if (usertype.equals("1")) {
            if (depars != null) {
                hashMap.put("deps", depars);
            }
            if (users != null) {
                hashMap.put("persons", users);
            }
        }
        hashMap.put("timetype", String.valueOf(timetype));
        if (timetype == 3) {
            hashMap.put("starTime", start_time);
            hashMap.put("endTime", end_time);
        }
        new BaseInternet().getData(URLString.STATISTICALBASEINFO, hashMap, StatisticBean.class, new StatisticEvent<StatisticBean>(), true);
    }

    @EventAnnotation
    public void eventResult(StatisticEvent<StatisticBean> event) {
        switch (event.getCode()) {
            case 200:
                //内勤定位数
                text_inside_location_num.setText(event.getData().getInlineCount());
                //外勤定位数
                text_out_location_num.setText(event.getData().getOutsideCount());
                //外勤补录数
                text_out_repair_num.setText(event.getData().getRetroactiveCount());
                //外勤定位率
                text_out_location_odds.setText(event.getData().getOutsideProbability().substring(0, event.getData().getOutsideProbability().length() - 1));
                //请假数
                text_leave_num.setText(event.getData().getLeaveCount());
                //外勤补录率
                text_out_repair_odds.setText(event.getData().getRetroactiveProbability().substring(0, event.getData().getRetroactiveProbability().length() - 1));
                break;
            case 1006:
                T.showShort(mactivity, "您的账号已在其它设备登录，请重登录");
                LittleUtils.exit(mactivity);
                startActivity(new Intent(mactivity, LoginActivity.class));
                break;
            default:
                T.showShort(getActivity(), event.getMsg());
                break;
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mactivity = activity;
    }

    /**
     * 初始化popWindows
     */
    private void initPop() {
        replay = new PopWindowManager(mactivity, R.layout.pop_without_address_date) {
            @Override
            public void onViewLister(View contentView, final PopWindowManager manager) {
                //本月
                final RelativeLayout rl_curmonth = (RelativeLayout) contentView.findViewById(R.id.pop_without_address_month);
                final ImageView iv_curmonth = (ImageView) contentView.findViewById(R.id.pop_without_address_month_img);
                //本季
                final RelativeLayout rl_curquarter = (RelativeLayout) contentView.findViewById(R.id.pop_select_date_bottom);
                final ImageView iv_curquarter = (ImageView) contentView.findViewById(R.id.pop_select_date_bottom_img);
                //本年
                final RelativeLayout rl_curyear = (RelativeLayout) contentView.findViewById(R.id.pop_select_personnel);
                final ImageView iv_curyear = (ImageView) contentView.findViewById(R.id.pop_select_personnel_img);
                //确定
                final TextView btn_sure = (TextView) contentView.findViewById(R.id.pop_select_change);
                //开始时间
                final TextView tv_starttime = (TextView) contentView.findViewById(R.id.pop_select_starttime_tv);
                //结束时间
                final TextView tv_endtime = (TextView) contentView.findViewById(R.id.pop_select_endtime_tv);

                contentView.findViewById(R.id.layout_gray).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        replay.hideWindow();
                    }
                });
            }
        };
    }

    /**
     * 显示popWindows
     */
    private void showPop(final String start_time, final String end_time, final int time_type) {
        replay = new PopWindowManager(mactivity, R.layout.pop_without_address_date) {
            @Override
            public void onViewLister(View contentView, final PopWindowManager manager) {
                //本月
                final RelativeLayout rl_curmonth = (RelativeLayout) contentView.findViewById(R.id.pop_without_address_month);
                final ImageView iv_curmonth = (ImageView) contentView.findViewById(R.id.pop_without_address_month_img);
                //本季
                final RelativeLayout rl_curquarter = (RelativeLayout) contentView.findViewById(R.id.pop_select_date_bottom);
                final ImageView iv_curquarter = (ImageView) contentView.findViewById(R.id.pop_select_date_bottom_img);
                //本年
                final RelativeLayout rl_curyear = (RelativeLayout) contentView.findViewById(R.id.pop_select_personnel);
                final ImageView iv_curyear = (ImageView) contentView.findViewById(R.id.pop_select_personnel_img);
                //确定
                final TextView btn_sure = (TextView) contentView.findViewById(R.id.pop_select_change);
                //开始时间
                final TextView tv_starttime = (TextView) contentView.findViewById(R.id.pop_select_starttime_tv);
                //结束时间
                final TextView tv_endtime = (TextView) contentView.findViewById(R.id.pop_select_endtime_tv);

                switch (time_type) {
                    //本月
                    case 0:
                        iv_curmonth.setVisibility(View.VISIBLE);
                        iv_curquarter.setVisibility(View.GONE);
                        iv_curyear.setVisibility(View.GONE);
                        break;
                    //本季
                    case 1:
                        iv_curmonth.setVisibility(View.GONE);
                        iv_curquarter.setVisibility(View.VISIBLE);
                        iv_curyear.setVisibility(View.GONE);
                        break;
                    //本年
                    case 2:
                        iv_curmonth.setVisibility(View.GONE);
                        iv_curquarter.setVisibility(View.GONE);
                        iv_curyear.setVisibility(View.VISIBLE);
                        break;
                    //自定义事件
                    case 3:
                        iv_curmonth.setVisibility(View.GONE);
                        iv_curquarter.setVisibility(View.GONE);
                        iv_curyear.setVisibility(View.GONE);
                        break;
                }

                if (!start_time.isEmpty()) {
                    tv_starttime.setText(start_time);

                } else {
                    tv_starttime.setText("请选择开始时间");
                }

                if (!end_time.isEmpty()) {
                    tv_endtime.setText(end_time);
                } else {
                    tv_endtime.setText("请选择结束时间");
                }

                contentView.findViewById(R.id.layout_gray).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        replay.hideWindow();
                    }
                });

                //本月
                rl_curmonth.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        iv_curmonth.setVisibility(View.VISIBLE);
                        iv_curquarter.setVisibility(View.GONE);
                        iv_curyear.setVisibility(View.GONE);

                        timetype = 0;
                    }
                });

                //本季
                rl_curquarter.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        iv_curmonth.setVisibility(View.GONE);
                        iv_curquarter.setVisibility(View.VISIBLE);
                        iv_curyear.setVisibility(View.GONE);

                        timetype = 1;
                    }
                });

                //本年
                rl_curyear.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        iv_curmonth.setVisibility(View.GONE);
                        iv_curquarter.setVisibility(View.GONE);
                        iv_curyear.setVisibility(View.VISIBLE);

                        timetype = 2;
                    }
                });

                //开始时间
                tv_starttime.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        iv_curmonth.setVisibility(View.GONE);
                        iv_curquarter.setVisibility(View.GONE);
                        iv_curyear.setVisibility(View.GONE);

                        starttime.showTimePickerView();
                        replay.hideWindow();
                        timetype = 3;
                    }
                });

                //结束时间
                tv_endtime.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        iv_curmonth.setVisibility(View.GONE);
                        iv_curquarter.setVisibility(View.GONE);
                        iv_curyear.setVisibility(View.GONE);

                        endtime.showTimePickerView();
                        replay.hideWindow();
                        timetype = 3;
                    }
                });

                //确定
                btn_sure.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch (timetype) {
                            case 0:
                                text_statistics_time.setText("本月");
                                break;
                            case 1:
                                text_statistics_time.setText("本季");
                                break;
                            case 2:
                                text_statistics_time.setText("本年");
                                break;
                            case 3:
                                text_statistics_time.setText("自定义时间");
                                break;
                        }
                        getInfo();
                        replay.hideWindow();
                    }
                });

            }
        };
        replay.showWindows(fg_leave_top, 0, 0);
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.fg_statistics_person, R.id.fg_statistics_date, R.id.fg_inside_location, R.id.fg_out_location,
            R.id.fg_out_repair, R.id.fg_out_location_odds, R.id.fg_leave_num, R.id.fg_out_repair_odds,
            R.id.fg_statistics_evection, R.id.fg_statistics_out_address, R.id.fg_statistics_leave_type, R.id.fg_statistics_time})
    public void onClick(View view) {
        switch (view.getId()) {
            //统计目标选择
            case R.id.fg_statistics_person:
                startActivityForResult(new Intent(mactivity, PersonnelSelectActivity.class), 0);
                break;
            //统计时间选择
            case R.id.fg_statistics_date:
                showPop(start_time, end_time, timetype);
                break;
            //内勤定位数
            case R.id.fg_inside_location:
                switch (jobs) {
                    case 0:     //普通员工
                        startActivity(new Intent(mactivity, HistoryRecordActivity.class)
                                .putExtra("type", "0").putExtra("timetype", timetype)
                                .putExtra("startTime", start_time).putExtra("endTime", end_time)
                                .putExtra("moldType", "0"));
                        break;
                    case 1:     //部门领导
                    case 2:     //分管领导
                    case 3:     //总经理
                        if (usertype.equals("0")) {
                            startActivity(new Intent(mactivity, HistoryRecordActivity.class)
                                    .putExtra("type", "0").putExtra("timetype", timetype)
                                    .putExtra("startTime", start_time).putExtra("endTime", end_time)
                                    .putExtra("moldType", "0"));
                        } else {
                            startActivity(new Intent(mactivity, WithoutDepNumActivity.class).putExtra("timetype", timetype)
                                    .putExtra("startTime", start_time).putExtra("endTime", end_time)
                                    .putExtra("moldType", "0").putExtra("code", 0));
                        }
                        break;
                }
                break;
            //外勤定位数
            case R.id.fg_out_location:
                switch (jobs) {
                    case 0:     //普通员工
                        startActivity(new Intent(mactivity, HistoryRecordActivity.class)
                                .putExtra("type", "0").putExtra("timetype", timetype)
                                .putExtra("startTime", start_time).putExtra("endTime", end_time)
                                .putExtra("moldType", "1"));
                        break;
                    case 1:     //部门领导
                    case 2:     //分管领导
                    case 3:     //总经理
                        if (usertype.equals("0")) {
                            startActivity(new Intent(mactivity, HistoryRecordActivity.class)
                                    .putExtra("type", "0").putExtra("timetype", timetype)
                                    .putExtra("startTime", start_time).putExtra("endTime", end_time)
                                    .putExtra("moldType", "1"));
                        } else {
                            startActivity(new Intent(mactivity, WithoutDepNumActivity.class).putExtra("timetype", timetype)
                                    .putExtra("startTime", start_time).putExtra("endTime", end_time)
                                    .putExtra("moldType", "1").putExtra("code", 1));
                        }
                        break;
                }
                break;
            //外勤补录数
            case R.id.fg_out_repair:
//                startActivity(new Intent(mactivity, OutRepairNumActivity.class));
                switch (jobs) {
                    case 0:     //普通员工
                        startActivity(new Intent(mactivity, HistoryRecordActivity.class)
                                .putExtra("type", "0").putExtra("timetype", timetype)
                                .putExtra("startTime", start_time).putExtra("endTime", end_time)
                                .putExtra("moldType", "2"));
                        break;
                    case 1:     //部门领导
                    case 2:     //分管领导
                    case 3:     //总经理
                        if (usertype.equals("0")) {
                            startActivity(new Intent(mactivity, HistoryRecordActivity.class)
                                    .putExtra("type", "0").putExtra("timetype", timetype)
                                    .putExtra("startTime", start_time).putExtra("endTime", end_time)
                                    .putExtra("moldType", "2"));
                        } else {
                            startActivity(new Intent(mactivity, WithoutDepNumActivity.class).putExtra("timetype", timetype)
                                    .putExtra("startTime", start_time).putExtra("endTime", end_time)
                                    .putExtra("moldType", "2").putExtra("code", 2));
                        }
                        break;
                }
                break;
            //外勤定位率
            case R.id.fg_out_location_odds:
//                startActivity(new Intent(mactivity, WithoutDepOddsActivity.class));
                break;
            //请假次数
            case R.id.fg_leave_num:
                startActivity(new Intent(mactivity, LeaveNumActivity.class));
                break;
            //外勤补录率
            case R.id.fg_out_repair_odds:
//                startActivity(new Intent(mactivity, OutRepairOddsActivity.class));
                break;
            //个人出差统计
            case R.id.fg_statistics_evection:
                startActivity(new Intent(mactivity, EvectionSearchActivity.class));
                break;
            //外勤地区统计
            case R.id.fg_statistics_out_address:
                startActivity(new Intent(mactivity, WithoutAddressActivity.class));
                break;
            //请假分类统计
            case R.id.fg_statistics_leave_type:
                startActivity(new Intent(mactivity, LeaveTypeStatisticsActivity.class));
                break;
            //按日期统计
            case R.id.fg_statistics_time:
                startActivity(new Intent(mactivity, DateStatisticsActivity.class));
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            return;
        } else {
            switch (requestCode) {
                case 0:
                    String type = data.getStringExtra("usertype");
                    text_statistics_person.setText(type);
                    if (type.equals("我自己")) {
                        usertype = "0";
                    } else if (type.equals("自选")) {
                        usertype = "1";
                    } else if (type.equals("全部")) {
                        usertype = "2";
                        if (data.getStringExtra("users") != null) {
                            users = data.getStringExtra("users");
                        } else {
                            users = null;
                        }
                        if (data.getStringExtra("depars") != null) {
                            depars = data.getStringExtra("depars");
                        } else {
                            depars = null;
                        }
                    }
                    getInfo();
                    break;
            }
        }
    }

    @Override
    protected void Resume() {
        getInfo();
        getMyInfo();
    }

    @Override
    public void initData() {

    }
}
